package persistence;

import model.Persona;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;

public class MongoDbManager {
	private static final MongoDbManager INSTANCE = new MongoDbManager();
	private final Datastore db;
	public static final String DB_NAME = "TestingMorphia";
	private MongoDbManager() {
		try {
			Mongo m = new Mongo("127.0.0.1", 27017);
			db = new Morphia().map(Persona.class).createDatastore(m, DB_NAME);
			//at application start
			db.ensureIndexes(); //creates indexes from @Index annotations in your entities
			db.ensureCaps(); //creates capped collections from @Entity
		}
		catch (Exception e) {
			throw new RuntimeException("Error initializing mongo db", e);
		}
	}
	public static MongoDbManager instance() {
		return INSTANCE;
		}
	public Datastore getDb() {
		return db;
	}
}